<?php

declare(strict_types=1);

namespace App\Service\Business;

use App\Service\Access\CustomModuleAccess;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 自定义模块-业务类
 */
class CustomModuleBusiness extends BaseBusiness
{
        #[Inject]
        protected CustomModuleAccess $CustomModuleAccess;

        /**
         * 创建
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function create(): array
        {
                $arrInsert = [
                        'customModuleId' => snowflakeId(),
                        'name' => $this->request->input('name'),
                        'limit' => $this->request->input('limit'),
                        'sort' => $this->request->input('sort', DEFAULT_SORT),
                        'createTime' => $this->request->getAttribute('timestamp'),
                ];
                $bool = $this->CustomModuleAccess->insert($arrInsert);
                if (!$bool) return businessError();
                $this->createOperateLog([], $arrInsert);
                return businessSuccess(['customModuleId' => $arrInsert['customModuleId']]);
        }

        /**
         * 更新
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function update(): array
        {
                $arrData = $this->CustomModuleAccess->find($this->request->input('customModuleId'), ['name', 'limit', 'sort', 'updateTime']);
                $arrUpdate = [
                        'customModuleId' => $this->request->input('customModuleId'),
                        'name' => $this->request->input('name'),
                        'limit' => $this->request->input('limit'),
                        'sort' => $this->request->input('sort', DEFAULT_SORT),
                        'updateTime' => $this->request->getAttribute('timestamp'),
                ];
                $intEffectRow = $this->CustomModuleAccess->updateById($arrUpdate);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData, $arrUpdate);
                return businessSuccess();
        }

        /**
         * 分页
         * @param array $inject
         * @return array
         */
        public function paginate(array $inject = []): array
        {
                $arrQueryResult = $this->CustomModuleAccess->paginate($inject);
                $arrResult = [
                        'page' => $arrQueryResult['current_page'],
                        'limit' => $arrQueryResult['per_page'],
                        'total' => $arrQueryResult['total'],
                        'list' => $arrQueryResult['data'],
                ];
                return businessSuccess($arrResult);
        }

        /**
         * 删除
         * @param array $inject
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function delete(array $inject = []): array
        {
                $arrData = $this->CustomModuleAccess->find($this->request->input('customModuleId'));
                $intEffectRow = $this->CustomModuleAccess->delete($inject ?: [
                        'wheres' => ['customModuleId' => $this->request->input('customModuleId')]
                ]);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData);
                return businessSuccess();
        }
}